# Dreams have to come true after all
from flask import g
from flask_restful import Resource
from flask_restful.reqparse import RequestParser

from Financial.resource.letter.serializer import LetterPaginateSerializer
from comment import db
from comment.models.User import User
from comment.models.letter import Letter_detail, Letter


class Letter(Resource):
    def post(self):
        # 发送信件
        rp = RequestParser()
        rp.add_argument('title', required=True)
        rp.add_argument('group', required=True) # 用户组
        rp.add_argument('content', required=True)
        args = rp.parse_args()
        title = args.get('title')
        group = int(args.get('group'))
        content = args.get('content')

        uid = g.user_id
        user = User.query.filter(User.id == uid).first()
        if user:
            # 创建新建详情记录
            new_letter_detail = Letter_detail(title=title, detail=content)
            db.session.add(new_letter_detail)
            db.session.flush()
        # 用户组按权限划分,goup值 2:全体用户 0:普通用户 1:管理组
        if group != 2:
            recList = User.query.filter(User.role == group).all()
        else:
            recList = User.query.all()
        for u in recList:
            # 根据筛选的用户组插入新建记录到数据库
            letter = Letter(sendID=user.username, recID=u.id, detail_id=new_letter_detail)
            db.session.add(letter)
            db.session.flush()
        db.session.commit()
        return {'msg': 'ok'}

    def get(self):
        rp = RequestParser()
        rp.add_argument('curPage', required=True)  # 标题
        rp.add_argument('perPage', required=True)  # 用户组
        args = rp.parse_args()
        curPage = int(args.get('curPage'))
        perPage = int(args.get('perPage'))

        uid = g.user_id
        user = User.query.filter(User.id == uid).first()
        if user:
            lettersList = Letter.query.filter(Letter.recID == user.id).paginate(curPage, perPage, error_out=False)
            data = LetterPaginateSerializer(lettersList).to_dict()
            return {'msg': 'ok', 'data': data, 'code': 200}
        else:
            return {'message': '未登录', 'code': 20008}

    def delete(self):
        rp = RequestParser()
        rp.add_argument('message_id', required=True)  # 标题
        args = rp.parse_args()
        m_id = args.get('message_id')

        letter = Letter.query.filter(Letter.id == m_id)
        if letter:
            letter.delete()
            db.session.commit()
            return {'msg': 'ok'}
        else:
            return {'message': '此消息不存在', 'code': 20005}

        pass

    # 更新新建
    def put(self):
        rp = RequestParser()
        rp.add_argument('message_id', requird=True)  # 标题
        args = rp.parse_args()
        m_id = args.get('message_id')

        letter = Letter.query.filter(Letter.id == m_id).first()
        if letter:
            letter.state = 1
            db.session.commit()
            return {'msg': 'ok'}
        else:
            return {'message': '此消息不存在', 'code': 20005}

        pass
